$(function () {
    function Admin() {
        this.ajaxUrl = '//practice-api.bjjixin.cn';
        this.locationVal = window.location;
        this.tooken = '';
        this.baseUrl = {
            // login
            login: this.ajaxUrl + '/home/v1/student/login',
            // loginuser
            loginuser: this.ajaxUrl + '/home/v1/student/index',
            // loginuser
            logout: this.ajaxUrl + '/home/v1/student/logout',
            // banner
            banner: this.ajaxUrl + '/home/v1/banner',
            // banner
            fixedGoods: this.ajaxUrl + '/home/v1/goods/fixedGoods',
            // 培训动态
            tidings: this.ajaxUrl + '/home/v1/tidings',
            // 培训动态
            teacher: this.ajaxUrl + '/home/v1/teacher',
            // 友情链接
            links: this.ajaxUrl + '/home/v1/links',
            // 商品列表
            goodslist: this.ajaxUrl + '/home/v1/goods',
            // 教学分类
            classify: this.ajaxUrl + '/common/v1/classify',
            // 行测分类
            classify_line: this.ajaxUrl + '/common/v1/classify_line',
            // 商品类别
            goods_assort: this.ajaxUrl + '/common/v1/index/goods_assort',
            // 商品详情
            detail: this.ajaxUrl + '/home/v1/goods/detail',
            // 获取指定商品下的题目
            topic: this.ajaxUrl + '/home/v1/goods/topic',
            // 获取指定商品下的所有视频
            videolist: this.ajaxUrl + '/home/v1/video/video-list',
            // 视频播放地址
            inspect: this.ajaxUrl + '/home/v1/video/inspect',
            // 人脸识别
            discern: this.ajaxUrl + '/home/v1/video/discern',
            //下单
            pay_create: this.ajaxUrl + '/home/v1/order/create',
            //二维码
            pay: this.ajaxUrl + '/home/v1/order/pay',
            //支付状态
            pay_status: this.ajaxUrl + '/home/v1/order/pay_status',
            // 
            order: this.ajaxUrl + '/home/v1/order'


        };
    };
    Admin.prototype.init = function () {
        this.eventFn();
        if (this.getCookierl('HOMEistooken')) {
            $('.login_btndis').text(this.getCookierl('HOMEusertxt')).removeClass('login_btn')
        }else{
            $('.line , .login_txt_b , .admin_a').hide();
        }


    };
    Admin.prototype.ajaxFn = function (ajaxList, callback) {
        var _this = this;
        $.ajax({
            url: ajaxList.url,
            type: ajaxList.type,
            dataType: 'json',
            data: ajaxList.data
            // ,
            // headers: {
            //     Authorization: _this.getCookierl('token_type') + ' ' + _this.getCookierl('cookval')
            // }
        }).done(function (res) {
            callback && callback(res)
        }).error(function (XHR, textStatus, errorThrown) {
            if (XHR.status === 400) {
                let responseJSON = jQuery.parseJSON(XHR.responseText);
                if (responseJSON.code === 40000) {
                    //TODO 提示框 直接去404
                    //layer.msg(responseJSON.message);
                    //setTimeout(function () {
                    window.location.href = '/error.html';
                    //}, 1000);

                } else if (responseJSON.code === 70002) {
                    alert('该课程为收费课程');
                    window.location.href = '/';
                }
            }
        })
    };
    Admin.prototype.ajaxHOMEFn = function (ajaxList, callback) {
        var _this = this;
        $.ajax({
            url: ajaxList.url,
            type: ajaxList.type,
            dataType: 'json',
            data: ajaxList.data,
            headers: {
                Authorization: _this.getCookierl('HOMEtoken_type') + ' ' + _this.getCookierl('HOMEcookval')
            }
        }).done(function (res) {
            callback && callback(res)
        }).error(function (XHR, textStatus, errorThrown) {
            let responseJSON = jQuery.parseJSON(XHR.responseText);
            if (XHR.status === 403) {
                if (responseJSON.code === 40003) {
                    alert('用户登录信息已失效');
                    window.location.href = '/login.html';
                }
            }
            if (XHR.status === 400) {
                if (responseJSON.code === 70002) {
                    alert('该课程为收费课程');
                    window.location.href = '/';
                }
            }

        });
    };
    // 获取cook
    Admin.prototype.getCookierl = function (name) {
        var cookieName = encodeURIComponent(name) + "="
            , cookieStart = document.cookie.indexOf(cookieName)
            , cookieValue = null;
        if (cookieStart > -1) {
            var cookieEnd = document.cookie.indexOf(";", cookieStart);
            if (cookieEnd == -1) {
                cookieEnd = document.cookie.length;
            }
            cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd));
        }
        return cookieValue;
    };
    // 设置cook
    Admin.prototype.setCookierl = function (name, value, expires, path, domain, secure) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expires);
        var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);
        cookieText += ";expires=" + exdate.toGMTString();
        if (path) {
            cookieText += ";path=" + path;
        }
        if (domain) {
            cookieText += ";domain=" + domain;
        }
        if (secure) {
            cookieText += ";secure";
        }
        document.cookie = cookieText;
    };
    // Admin.prototype.clearCookierl = function (name) {
    //     this.setCookierl(name, "", -1);
    // };
    Admin.prototype.clearCookierl = function (name) {
        var keys = document.cookie.match(/[^ =;]+(?==)/g);
        if (!keys || !keys.length) return;
        for (var i = keys.length; i--;) {
            document.cookie = keys[i] + '=0;expires=Thu, 01 Jan 1970 00:00:00 GMT;';
        }
    };
    Admin.prototype.eventFn = function () {
        var _this = this


    };
    Admin.prototype.getQueryVariable = function (variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
        return (false);
    };


    HOME = new Admin();
    HOME.init();
});